<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Logic\Api\AuthLogic;
use App\Http\Logic\Api\LoginLogic;
use App\Http\Requests\Api\Auth\Login;
use App\Http\Requests\Api\Auth\UserPhone;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;

class AuthController extends Controller
{
    protected AuthLogic $authLogic;

    public function __construct(AuthLogic $authLogic)
    {
        $this->authLogic = $authLogic;
    }

    /**
     * @throws GuzzleException
     */
    public function login(Login $login): JsonResponse
    {
        try {
            $params = $login->validated();

            return $this->success(
                $this->authLogic->login($params['code'])
            );
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * @throws GuzzleException
     */
    public function userPhone(UserPhone $userPhone): JsonResponse
    {
        try {
            $params = $userPhone->validated();

            return $this->success(
                $this->authLogic->userPhone($params['code'])
            );
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }
}
